package com.amadeus.personal_demo;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;

import java.util.Locale;

public class LanguageUtil {

    private static final String KEY_APP_LANGUAGE = "KEY_APP_LANGUAGE";

    public static void changeAppLanguage(Context context, Locale locale,boolean change){

        Resources resources = context.getResources();
        DisplayMetrics sizes = resources.getDisplayMetrics();
        Configuration configuration = resources.getConfiguration();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
            configuration.setLocale(locale);
        }else {
            configuration.locale = locale;
        }

        resources.updateConfiguration(configuration,sizes);
        if (change){

        }
    }

    public static void saveLanguageSetting(Context context,Locale locale){
        Language.putdata(context,KEY_APP_LANGUAGE,locale.getLanguage());
    }

    public static String getAppLanguage(Context context){
        return (String)Language.get(context,KEY_APP_LANGUAGE,Locale.getDefault().getLanguage());
    }

    public static Locale getAppLocale(Context context){
        String lang = (String)Language.get(context,KEY_APP_LANGUAGE,Locale.getDefault().getLanguage());
        if (!lang.equals(Locale.SIMPLIFIED_CHINESE.getLanguage())&&!lang.equals(Locale.US.getLanguage())){
            lang = Locale.SIMPLIFIED_CHINESE.getLanguage();
        }
        Locale locale = new Locale(lang);
        return locale;
    }

}
